package com.zsyh.jiujiu.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.zyh.toolslib.base.BaseActivity;
import com.example.zyh.toolslib.utils.EmptyUtil;
import com.example.zyh.toolslib.utils.SPUtils;
import com.example.zyh.toolslib.widget.TitleBar;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.MessageEvn;
import com.zsyh.jiujiu.net.MyObserver;
import com.zsyh.jiujiu.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import retrofit2.http.Query;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class QuernOrderActtivity extends BaseActivity {


    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.jine)
    EditText jine;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.adress)
    TextView adress;
    @BindView(R.id.queeding)
    TextView queeding;
    @BindView(R.id.re_adress)
    RelativeLayout reAdress;
    private final static int REQUESTCODE = 1; // 返回的结果码
    private String userId,orderid;


    @Override
    protected int initLayout() {
        return R.layout.activity_querendingdan;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        orderid=getIntent().getStringExtra("id");
        userId=(String) SPUtils.get(thisActivity, "userid", "");
    }

    @Override
    protected void initEvent() {
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    @OnClick({R.id.re_adress, R.id.queeding})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.re_adress:
                Intent intent = new Intent(this, AdressManger.class);
                startActivityForResult(intent, REQUESTCODE);
                break;
            case R.id.queeding:
                if(EmptyUtil.isEmpty(jine.getText().toString())){
                    ToastUtils.showShort(thisActivity,"确认金额");
                    return;
                }
                if(EmptyUtil.isEmpty(name.getText().toString())){
                    ToastUtils.showShort(thisActivity,"选择地址");
                    return;
                }
                SureOrder(userId,orderid,jine.getText().toString(),name.getText().toString(),phone.getText().toString(),adress.getText().toString());
                break;
        }
    }

    /**
     * @param
     * @param
     * @param
     */

    public void SureOrder(String userId,
                          String orderId,
                          String money,
                          String username,
                          String phone,
                          String address) {
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .confirmOrder(userId,
                        orderId,
                        money,
                        username,
                        phone,
                        address)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this, true) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        EventBus.getDefault().post(new MessageEvn("message", "all"));
                        thisActivity.finish();

                    }
                });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 2) {
            if (requestCode == REQUESTCODE) {
                String names = data.getStringExtra("name");
                String phones = data.getStringExtra("phone");
                String adressd = data.getStringExtra("adress");
                String id = data.getStringExtra("id");
                String ismoren = data.getStringExtra("ismoren");

                phone.setText(phones);
                name.setText(names);
                adress.setText(adressd);


            }
        }
    }
}
